package com.service.healthycode.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.healthycode.dto.UserDto;
import com.service.healthycode.entity.UserLoginRecords;
import com.service.healthycode.service.DangerUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static com.service.config.constant.Constant.USER_NOT_LOGIN;

/**
 * @author: hjl
 * @date: 2022/04/17 21:09
 * @description
 */
@CrossOrigin
@RestController
@RequestMapping("/dangerUser")
@Api(value = "风险用户",tags = "风险用户")
public class DangerUserController {
    @Resource
    private DangerUserService dangerService;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation(value = "通过mac地址查询人员轨迹")
    @RequestMapping(value = "/queryDangerUserByMac",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryDangerUserByMac(@RequestParam("mac") String mac,
                                       @RequestParam("startTime") String startTime,
                                       @RequestParam("endTime") String endTime,
                                       @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.queryDangerUserByMac(mac, startTime,endTime);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查询今日新增")
    @RequestMapping(value = "/queryIncrementOfToday",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryIncrementOfToday(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.queryIncrementOfToday();
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查询指定风险人员行程轨迹")
    @RequestMapping(value = "/queryDangerUserById",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryDangerUserById(@RequestParam("id")String id,
                                      @RequestParam("origin") Integer origin,
                                      @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.queryDangerUserById(id,origin);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");


    }

    @ApiOperation(value = "导出风险清单")
    @RequestMapping(value ="/exportDangerList",method = RequestMethod.GET)
    @ResponseBody
    public Result exportDangerList(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.exportDangerList();
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");

    }

    @ApiOperation(value = "导出筛选后的风险清单")
    @RequestMapping(value ="/exportDangerListByCondition",method = RequestMethod.GET)
    @ResponseBody
    public Result exportDangerListByCondition(@RequestParam("mac") String mac,
                                              @RequestParam("startTime") String startTime,
                                              @RequestParam("endTime") String endTime,
                                              @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.exportDangerListByCondition(mac,startTime,endTime);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查看管理员录入的风险终端")
    @RequestMapping(value = "/queryDangerByAdmin",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryDangerByAdmin(@RequestParam(value = "page",required = true) Integer page,
                                     @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.queryDangerByAdmin(page);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查看下一级")
    @RequestMapping(value = "/queryNextLevel",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryNextLevel(@RequestParam("ids") String[] ids,
                                 @RequestParam("origin") Integer origin,
                                 @RequestParam(value = "page",required = true) Integer page,
                                 @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                List<String> idList = Arrays.asList(ids);
                return dangerService.queryNextLevel(idList,origin,page);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查看上一级")
    @RequestMapping(value = "/queryAboveLevel",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryAboveLevel(@RequestParam(value = "ids",required = true) List<String> ids,
                                 @RequestParam("origin") Integer origin,
                                  @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.queryAboveLevel(ids,origin);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "通过mac地址查询终端状态")
    @RequestMapping(value = "/queryStatusByMac",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryByMac(@RequestParam("mac") String mac,
                             @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserDto userDto = JsonXMLUtils.json2obj(value, UserDto.class);
                return dangerService.queryByMac(mac);
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
